package com.h4313.ghome.ui.fragments;

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.h4313.ghome.R;
import com.h4313.ghome.io.MorningConfTask;
import com.h4313.ghome.io.MorningConfTask.MorningConfCallback;
import com.h4313.ghome.ui.dialog.HourPickerDialog;
import com.h4313.ghome.ui.dialog.HourPickerDialog.MorningConf;
import com.h4313.ghome.ui.dialog.HourPickerDialog.TimePickerCallback;
import com.h4313.ghome.utils.UIUtils;

public class MorningConfFragment extends ModuleFragment implements Refreshable, MorningConfCallback, TimePickerCallback, OnClickListener {

	TextView mCoffee, mGCalendar;
	MorningConf mConf = new MorningConf();
	ProgressBar mRefresh;
	View mCurrentPicking;
	
	public static MorningConfFragment newInstance(String name, int id) {
		
		MorningConfFragment frag = new MorningConfFragment();
		Bundle b = new Bundle();
		b.putInt(PARAM_ID, id);
		b.putString(PARAM_NAME, name);
		frag.setArguments(b);
		return frag;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		refresh();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_morning_conf, container, false);
		
		mCoffee = (TextView) v.findViewById(R.id.morning_coffee);
		mCoffee.setOnClickListener(this);
		mGCalendar = (TextView) v.findViewById(R.id.morning_gcalendar);
		mGCalendar.setOnClickListener(this);
		mRefresh = (ProgressBar) v.findViewById(R.id.refresh);
		
		v.findViewById(R.id.morning_submit).setOnClickListener(this);
		
		return v;
	}
	

	@Override
	public void onConfRetreived(MorningConf conf, int type) {
		mRefresh.setVisibility(View.INVISIBLE);
		
		if (type == MorningConfTask.TYPE_GET) {
			if (conf != null) {
				mCoffee.setText(UIUtils.formatHour(conf.hourCoffee, conf.minCoffee));
				mGCalendar.setText(UIUtils.formatHour(conf.hourGCalendar ,conf.minGCalendar));
				mConf = conf;
			} else {
				UIUtils.showToast(this, "Erreur récupération de la configuration Morning", Toast.LENGTH_SHORT);
			}
		} else if (type == MorningConfTask.TYPE_PUT) {
			if (conf != null) {
				UIUtils.showToast(this, "Mise à jour Morning réussie avec succès", Toast.LENGTH_SHORT);
			} else {
				UIUtils.showToast(this, "Erreur mise à jour Morning", Toast.LENGTH_SHORT);
			}
		}
	}
	
	@Override
	public void onTimeSet(int hour, int min) {
		if (mCurrentPicking == mCoffee) {
			
			mConf.hourCoffee = hour;
			mConf.minCoffee = min;
			mCoffee.setText(UIUtils.formatHour(hour, min));
			
			mCurrentPicking = null;
		} else if (mCurrentPicking == mGCalendar) {
			
			mConf.hourGCalendar = hour;
			mConf.minGCalendar = min;
			mGCalendar.setText(UIUtils.formatHour(hour, min));
			
			mCurrentPicking = null;
		}
	}

	@Override
	public void onClick(View view) {
		if (view == mCoffee) {
			DialogFragment newFragment = new HourPickerDialog(this);
			newFragment.show(getFragmentManager(), "timepicker_coffee");
			mCurrentPicking = view;
		} else if (view == mGCalendar) {
			DialogFragment newFragment = new HourPickerDialog(this);
			newFragment.show(getFragmentManager(), "timepicker_gcalendar");
			mCurrentPicking = view;
		} else if (view.getId() == R.id.morning_submit) {
			mRefresh.setVisibility(View.VISIBLE);
			MorningConfTask task = new MorningConfTask(this, getModuleId());
			task.doPut(mConf);
		}
	}

	@Override
	public void refresh() {
		mRefresh.setVisibility(View.VISIBLE);
		MorningConfTask task = new MorningConfTask(this, getModuleId());
		task.doGet();		
	}
}
